/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.madding.shared.encrypt.cert.bc.util;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import org.bouncycastle.asn1.DERObjectIdentifier;
import org.bouncycastle.jce.X509Principal;

/**
 * 类X509PrincipalUtil.java的实现描述：x509规则构造类
 * 
 * @author madding.lip Dec 5, 2013 4:39:19 PM
 */
public class X509PrincipalUtil {

    private static final String DN_C         = "CN";
    private static final String DN_ST        = "ZheJiang";
    private static final String DN_L         = "HangZhou";
    private static final String DN_O         = "Alibaba";
    private static final String DN_OU        = "Inc";
    private static final String DN_CN_ROOT   = "Alilang CA";
    private static final String DN_CN_CLIENT = "Alilang Client CA";
    private static final String DN_E_ROOT    = "alilang@alibaba-inc.com";

    public static X509Principal createMasterPrincipal() {
        Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
        Vector<DERObjectIdentifier> order = new Vector<DERObjectIdentifier>();
        // 添加属性
        attrs.put(X509Principal.C, DN_C);
        attrs.put(X509Principal.ST, DN_ST);
        attrs.put(X509Principal.L, DN_L);
        attrs.put(X509Principal.O, DN_O);
        attrs.put(X509Principal.OU, DN_OU);
        attrs.put(X509Principal.CN, DN_CN_ROOT);
        attrs.put(X509Principal.EmailAddress, DN_E_ROOT);

        // 属性排序
        order.addElement(X509Principal.EmailAddress);
        order.addElement(X509Principal.CN);
        order.addElement(X509Principal.OU);
        order.addElement(X509Principal.O);
        order.addElement(X509Principal.L);
        order.addElement(X509Principal.ST);
        order.addElement(X509Principal.C);

        return new X509Principal(order, attrs);
    }

    public static X509Principal createClientIntermediatePrincipal() {
        Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
        Vector<DERObjectIdentifier> order = new Vector<DERObjectIdentifier>();

        // 添加属性
        attrs.put(X509Principal.C, DN_C);
        attrs.put(X509Principal.ST, DN_ST);
        attrs.put(X509Principal.L, DN_L);
        attrs.put(X509Principal.O, DN_O);
        attrs.put(X509Principal.OU, DN_OU);
        attrs.put(X509Principal.CN, DN_CN_CLIENT);
        attrs.put(X509Principal.EmailAddress, DN_E_ROOT);

        // 属性排序
        order.addElement(X509Principal.EmailAddress);
        order.addElement(X509Principal.CN);
        order.addElement(X509Principal.OU);
        order.addElement(X509Principal.O);
        order.addElement(X509Principal.L);
        order.addElement(X509Principal.ST);
        order.addElement(X509Principal.C);

        return new X509Principal(order, attrs);
    }

    public static X509Principal createClientUserPrincipal(String cn, String email, String title) {
        // subjects name table.
        Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
        Vector<DERObjectIdentifier> order = new Vector<DERObjectIdentifier>();

        // 添加属性
        attrs.put(X509Principal.C, DN_C);
        attrs.put(X509Principal.ST, DN_ST);
        attrs.put(X509Principal.L, DN_L);
        attrs.put(X509Principal.O, DN_O);
        attrs.put(X509Principal.OU, DN_OU);
        attrs.put(X509Principal.CN, cn);
        attrs.put(X509Principal.EmailAddress, email);
        attrs.put(X509Principal.T, title);

        // 属性排序
        order.addElement(X509Principal.EmailAddress);
        order.addElement(X509Principal.CN);
        order.addElement(X509Principal.T);
        order.addElement(X509Principal.OU);
        order.addElement(X509Principal.O);
        order.addElement(X509Principal.L);
        order.addElement(X509Principal.ST);
        order.addElement(X509Principal.C);

        return new X509Principal(order, attrs);
    }

    public static X509Principal createClientUserPrincipal(String cn, String email) {
        // subjects name table.
        Hashtable<DERObjectIdentifier, String> attrs = new Hashtable<DERObjectIdentifier, String>();
        Vector<DERObjectIdentifier> order = new Vector<DERObjectIdentifier>();

        // 添加属性
        attrs.put(X509Principal.C, DN_C);
        attrs.put(X509Principal.ST, DN_ST);
        attrs.put(X509Principal.L, DN_L);
        attrs.put(X509Principal.O, DN_O);
        attrs.put(X509Principal.OU, DN_OU);
        attrs.put(X509Principal.CN, cn);
        attrs.put(X509Principal.EmailAddress, email);

        // 属性排序
        order.addElement(X509Principal.EmailAddress);
        order.addElement(X509Principal.CN);
        order.addElement(X509Principal.OU);
        order.addElement(X509Principal.O);
        order.addElement(X509Principal.L);
        order.addElement(X509Principal.ST);
        order.addElement(X509Principal.C);

        return new X509Principal(order, attrs);
    }

    public static X509Principal createClientUserPrincipal(List<String> cnList, String email) {
        // subjects name table.
        Vector<DERObjectIdentifier> attrKey = new Vector<DERObjectIdentifier>();
        Vector<String> attrValue = new Vector<String>();

        // 添加属性
        attrKey.addElement(X509Principal.EmailAddress);
        attrValue.addElement(email);
        for (String cn : cnList) {
            attrKey.addElement(X509Principal.CN);
            attrValue.addElement(cn);
        }
        attrKey.addElement(X509Principal.OU);
        attrValue.addElement(DN_OU);
        attrKey.addElement(X509Principal.O);
        attrValue.addElement(DN_O);
        attrKey.addElement(X509Principal.L);
        attrValue.addElement(DN_L);
        attrKey.addElement(X509Principal.ST);
        attrValue.addElement(DN_ST);
        attrKey.addElement(X509Principal.C);
        attrValue.addElement(DN_C);

        return new X509Principal(attrKey, attrValue);
    }

}
